package com.tcreditdemo;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private FrameLayout fl;
    private RadioButton rdbt1;
    private RadioButton rdbt2;
    private RadioGroup radiogroup;
    private RelativeLayout rlroot;
    public ArrayList<Fragment> fragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initialize();

    }

    private void initialize() {
        initFragment();
        fl = (FrameLayout) findViewById(R.id.fl);
        rdbt1 = (RadioButton) findViewById(R.id.rdbt1);
        rdbt2 = (RadioButton) findViewById(R.id.rdbt2);
        radiogroup = (RadioGroup) findViewById(R.id.radiogroup);
        rlroot = (RelativeLayout) findViewById(R.id.rlroot);
        Drawable me_icon = getResources().getDrawable(R.drawable.select_radio_home);
        me_icon.setBounds(0,0,getResources().getDimensionPixelOffset(R.dimen.x20),getResources().getDimensionPixelOffset(R.dimen.x20));
        rdbt1.setCompoundDrawables(null,me_icon,null,null);
        Drawable home_icon = getResources().getDrawable(R.drawable.select_radio_me);
        home_icon.setBounds(0,0,getResources().getDimensionPixelOffset(R.dimen.x20),getResources().getDimensionPixelOffset(R.dimen.x20));
        rdbt1.setCompoundDrawables(null,me_icon,null,null);
        rdbt2.setCompoundDrawables(null,home_icon,null,null);
        rdbt1.setChecked(true);
        rdbt1.setOnClickListener(this);
        rdbt2.setOnClickListener(this);
        switchFragment(0);

    }

    public  void switchFragment(int j) {
        FragmentTransaction beginTransaction = getSupportFragmentManager().beginTransaction();
        for (int i = 0; i < fragments.size(); i++) {
            Fragment f = fragments.get(i);
            if (i == j) {
                if (f.isAdded()) {
                    //已经添加过，直接展示
                    beginTransaction.show(f);
                } else {
                    //如果没有添加，则把fragment添加进来
                    beginTransaction.add(R.id.fl,f,f.getClass().getSimpleName());
                }
            } else {
                if (f.isAdded()) {
                    //当遍历的和当前的不相等的时候，把这些全部隐藏起来。
                    beginTransaction.hide(f);
                }
            }
        }
        //最后一定要记得提交
        beginTransaction.commit();
    }


    public void initFragment() {
        LogUtil.e("BeginActivity", "执行了么");
        fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new MeFragment());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.rdbt1:
                switchFragment(0);
                break;
            case R.id.rdbt2:
                switchFragment(1);
                break;
        }
    }
}
